<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小西瓜 <751707632@qq.com>
// +----------------------------------------------------------------------
namespace app\fowl\controller;

use cmf\controller\AdminBaseController;
use think\Db;
use app\admin\model\ThemeModel;
use app\fowl\model\ImsXbFindModel;
use app\portal\model\PortalPostModel;
use app\portal\service\PostService;
use app\portal\model\PortalCategoryModel;

class AdminRealizeController extends AdminBaseController
{
    /**
     * 音频列表
     * @adminMenu(
     *     'name'   => '学吧',
     *     'parent' => 'fowl/AdminRealize/default',
     *     'remark' => '发现',
     * )
     */
    public function index()
    {
        $param = $this->request->param();

        $categoryId = $this->request->param('category', 0, 'intval');
        $startTime = empty($param['start_time']) ? 0 : strtotime($param['start_time']);
        $endTime   = empty($param['end_time']) ? 0 : strtotime($param['end_time']);
        if (!empty($startTime) && !empty($endTime)) {
            $where['create_time'] = [['>= time', $startTime], ['<= time', $endTime]];
        } else {
            if (!empty($startTime)) {
                $where['create_time'] = ['>= time', $startTime];
            }
            if (!empty($endTime)) {
                $where['create_time'] = ['<= time', $endTime];
            }
        }
        if(!empty($categoryId)){
            $where['type_id'] = $categoryId;
        }
        $keyword = empty($param['keyword']) ? '' : $param['keyword'];
        if (!empty($keyword)) {
            $where['title'] = ['like', "%$keyword%"];
        }

        $where['status'] = 2;

        $categorys = Db::name('portal_category')->where('parent_id', 4)->field('id,name')->select();
        foreach($categorys as $cv){
            $cvdata[] = '<option value="'.$cv['id'].'">'.$cv['name'].'</option>';
        }
        $cvstr = implode(',', $cvdata);

        $portalPost = new ImsXbFindModel();
        if(isset($where)){
            $result = $portalPost->where($where)->paginate(10);
        }else{
            $result = $portalPost->paginate(10);
        }


        if(!empty($result)){
            foreach($result as $vs){
                $vs->getuserinfo;
                $vs->getcategoryinfo;
            }
        }

//        return json($result->items());
        $item = $result->items();
        if(!empty($item)){
            foreach ($item as $iv){
                $iv['cover_pic'] = cmf_get_image_url(str_replace("\\","/",$iv['cover_pic']));
                //http://znxt.com/upload/uploads/20180821/9604b9e0e66ee3391944b97ff0d2cb2a.jpg
                $ivs[] = $iv;
            }
        }
        $ivs = isset($ivs) ? $ivs : '';
        //$this->assign('articles', $result->items());
        $this->assign('articles', $ivs);
        $this->assign('page', $result->render());
        $this->assign('categorys', $cvstr);
        return $this->fetch();
    }

    /**
     * 视频详情
     */
    public function detail()
    {
        $param = $this->request->param('id');
        return $this->fetch();
    }



    /**
     * 音频置顶
     * @adminMenu(
     *     'name'   => '音频置顶',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '音频置顶',
     *     'param'  => ''
     * )
     */
    public function top()
    {
        $param           = $this->request->param();
        $portalPostModel = new ImsXbFindModel();

        if (isset($param['ids']) && isset($param["yes"])) {
            $ids = $this->request->param('ids/a');
            $portalPostModel->where(['id' => ['in', $ids]])->update(['is_top' => 1]);
            $this->traceLog('音频置顶', $param); // 操作日志
            $this->success("置顶成功！", '');
        }

        if (isset($_POST['ids']) && isset($param["no"])) {
            $ids = $this->request->param('ids/a');
            $this->traceLog('音频取消置顶', $param); // 操作日志
            $portalPostModel->where(['id' => ['in', $ids]])->update(['is_top' => 2]);
            $this->success("取消置顶成功！", '');
        }
    }



    /**
     * 话题推荐
     * @adminMenu(
     *     'name'   => '话题推荐',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '话题推荐',
     *     'param'  => ''
     * )
     */
    public function recommend()
    {
        $param           = $this->request->param();
        $portalPostModel = new ImsXbFindModel();
        if (isset($param['ids']) && isset($param["yes"])) {
            $ids = $this->request->param('ids/a');
            $portalPostModel->where(['id' => ['in', $ids]])->update(['recommended' => 1]);
            $this->traceLog('音频推荐', $param); // 操作日志
            $this->success("推荐成功！", '');
        }
        if (isset($param['ids']) && isset($param["no"])) {
            $ids = $this->request->param('ids/a');
            $portalPostModel->where(['id' => ['in', $ids]])->update(['recommended' => 0]);
            $this->traceLog('音频取消推荐', $param); // 操作日志
            $this->success("取消推荐成功！", '');

        }
    }


    /**
     * 话题删除
     * @adminMenu(
     *     'name'   => '话题删除',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '话题删除',
     *     'param'  => ''
     * )
     */
    public function delete()
    {
        $param           = $this->request->param();
        $portalPostModel = new ImsXbFindModel();

        if (isset($param['id'])) {
            $id           = $this->request->param('id', 0, 'intval');
            $result       = $portalPostModel->where(['id' => $id])->find();
            $resultPortal = $portalPostModel
                ->where(['id' => $id])
                ->update(['delete_time' => date('Y-m-d H:i:s', time()), 'update_time' => date('Y-m-d H:i:s', time())]);
            $this->traceLog('音频删除', $param); // 操作日志
            $this->success("删除成功！", '');
        }

        if (isset($param['ids'])) {
            $ids     = $this->request->param('ids/a');
            $recycle = $portalPostModel->where(['id' => ['in', $ids]])->select();
            $result  = $portalPostModel->where(['id' => ['in', $ids]])->update(['delete_time' => date('Y-m-d H:i:s', time()),  'update_time' => date('Y-m-d H:i:s', time())]);
            $this->traceLog('回收音频', $param); // 操作日志
            $this->success("删除成功！", '');
        }
    }

    /**
     * 编辑话题
     * @adminMenu(
     *     'name'   => '编辑话题',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '编辑话题',
     *     'param'  => ''
     * )
     */
    /**
     * 会议回放
     */
    public function edit()
    {
        error_reporting(E_ERROR | E_PARSE); // 定义错误级别
        $get = $this->request->param();
        if ( !$get['vm_id'] ) {
            $this->error('缺少参数');
        }

        $data = Db::name('ims_xb_find')->where('id', $get['vm_id'])->field('id, title, vedio_link')->find();

        $this->assign([
            'data' => $data,
        ]);
        return $this->fetch();
    }


    /**
     * 添加话题
     * @adminMenu(
     *     'name'   => '添加话题',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加话题',
     *     'param'  => ''
     * )
     */
    public function add()
    {
        $themeModel        = new ThemeModel();
        $articleThemeFiles = $themeModel->getActionThemeFiles('portal/Article/index');
        $this->assign('article_theme_files', 'fds');
        return $this->fetch();
    }



    /**
     * 话题分类选择对话框
     * @adminMenu(
     *     'name'   => '话题分类选择对话框',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '话题分类选择对话框',
     *     'param'  => ''
     * )
     */
    public function select()
    {
        $ids                 = $this->request->param('ids');
        $selectedIds         = explode(',', $ids);
        $portalCategoryModel = new PortalCategoryModel();

        $tpl = <<<tpl
<tr class='data-item-tr'>
    <td>
        <input type='checkbox' class='js-check' data-yid='js-check-y' data-xid='js-check-x' name='ids[]'
               value='\$id' data-name='\$name' \$checked>
    </td>
    <td>\$id</td>
    <td>\$spacer <a href='\$url' target='_blank'>\$name</a></td>
</tr>
tpl;

        $categoryTree = $portalCategoryModel->adminCategoryTableTree($selectedIds, $tpl);

        $where      = ['delete_time' => 0];
        $categories = $portalCategoryModel->where($where)->select();

        $this->assign('categories', $categories);
        $this->assign('selectedIds', $selectedIds);
        $this->assign('categories_tree', $categoryTree);
        return $this->fetch();
    }


    /**
     * 编辑话题分类提交
     * @adminMenu(
     *     'name'   => '编辑话题分类提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '编辑话题分类提交',
     *     'param'  => ''
     * )
     */
    public function editPost()
    {
        $data = $this->request->param();
        return json($data);
        $result = $this->validate($data, 'PortalCategory');

        if ($result !== true) {
            $this->error($result);
        }

        $portalCategoryModel = new PortalCategoryModel();

        $result = $portalCategoryModel->editCategory($data);

        if ($result === false) {
            $this->error('保存失败!');
        }

        $this->success('保存成功!');
    }

    public function publish()
    {
        $param           = $this->request->param();
        $portalPostModel = new ImsXbFindModel();
        if (isset($param['ids']) && isset($param["yes"])) {
            $ids = $this->request->param('ids/a');
            $portalPostModel->where(['id' => ['in', $ids]])->update(['status' => 2, 'update_time' => time()]);
            $this->traceLog('音频审核成功', $param); // 操作日志
            $this->success("审核成功！", '');
        }

        if (isset($param['ids']) && isset($param["no"])) {
            $ids = $this->request->param('ids/a');
            $portalPostModel->where(['id' => ['in', $ids]])->update(['status' => 1, 'update_time' => time()]);
            $this->traceLog('取消音频审核', $param); // 操作日志
            $this->success("取消审核成功！", '');
        }
    }
}
